package com.h2n.framework.dao.classes;

import com.h2n.framework.dao.IModuleDAO;
import com.h2n.framework.dao.ConstantsDAO;
import com.h2n.framework.dto.ModuleDTO;
import com.h2n.framework.dto.MemberModuleDTO;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * Created by IntelliJ IDEA.
 * User: NhatNV
 * Date: Sep 23, 2009
 * Time: 11:02:15 PM
 * To change this template use File | Settings | File Templates.
 */
public class ModuleDAO implements IModuleDAO {
  private JdbcTemplate jdbcTemplate;
  public void setJdbcTemplate(JdbcTemplate _jdbcTemplate){
    jdbcTemplate = _jdbcTemplate;
  }

  @Override
  public long insert(ModuleDTO _moduleDto) throws Exception {
    return 0;  //To change body of implemented methods use File | Settings | File Templates.
  }

  @Override
  public void update(ModuleDTO _moduleDto) throws Exception {
    //To change body of implemented methods use File | Settings | File Templates.
  }

  @Override
  public void delete(int _id) throws Exception {
    //To change body of implemented methods use File | Settings | File Templates.
  }

  @Override
  public ModuleDTO getInfo(int _id) throws Exception {
    return null;  //To change body of implemented methods use File | Settings | File Templates.
  }

  @Override
  public List<ModuleDTO> getAll() throws Exception {
    return null;  //To change body of implemented methods use File | Settings | File Templates.
  }

  @Override
  public List<ModuleDTO> getChildByMemberAndParentId(int _memberId, int _parentId) throws Exception {
    jdbcTemplate.setResultsMapCaseInsensitive(true);
    SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate).withFunctionName(ModuleDTO.GET_CHILD_BY_MEMBER_AND_PARENTID_PROCEDURE).returningResultSet(ConstantsDAO.RESULT_SET, ParameterizedBeanPropertyRowMapper.newInstance(ModuleDTO.class));
    Map inMap  = new HashMap();
    inMap.put(MemberModuleDTO.Parameters.p_member_id.toString(),_memberId);
    inMap.put(ModuleDTO.Parameters.p_parent_id.toString(),_parentId);
    SqlParameterSource inParams = new MapSqlParameterSource(inMap);
    Map outMap = simpleJdbcCall.execute(inParams);

    return (List)outMap.get(ConstantsDAO.RESULT_SET);
  }

  @Override
  public List<ModuleDTO> getParentByMemberId(int _memberId) throws Exception {
    jdbcTemplate.setResultsMapCaseInsensitive(true);
    SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate).withFunctionName(ModuleDTO.GET_PARENT_BY_MEMBER_ID_PROCEDURE).returningResultSet(ConstantsDAO.RESULT_SET, ParameterizedBeanPropertyRowMapper.newInstance(ModuleDTO.class));
    Map inMap  = new HashMap();
    inMap.put(MemberModuleDTO.Parameters.p_member_id.toString(),_memberId);
    SqlParameterSource inParams = new MapSqlParameterSource(inMap);
    Map outMap = simpleJdbcCall.execute(inParams);

    return (List)outMap.get(ConstantsDAO.RESULT_SET);
  }
}
